package com.edu.service.impl;

import com.edu.dao.AddressesDao;
import com.edu.dao.AddressesDetailDao;
import com.edu.dao.impl.AddressesDaoImpl;
import com.edu.dao.impl.AddressesDetailDaoImpl;
import com.edu.entity.Address;
import com.edu.entity.AddressEntity;
import com.edu.service.AddressesService;
import com.edu.utils.DatabaseManager;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * com.edu.service.impl
 * 2024/7/15 8:52
 *
 * @author Fancy
 * @version 1.0
 */
public class AddressesServiceImpl implements AddressesService {

    private AddressesDao addressesDao;
    private AddressesDetailDao addressesDetailDao;
    private Connection connection;

    public AddressesServiceImpl() {
        try {
            connection = DatabaseManager.getInstance().getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        this.addressesDao = new AddressesDaoImpl(connection);
        this.addressesDetailDao = new AddressesDetailDaoImpl(connection);
    }

    @Override
    public Boolean addAddress(AddressEntity address, int userId) {
        try {
            connection.setAutoCommit(false);
            Integer addAddressDetailId = addressesDetailDao.addAddressDetail(address);
            if (addAddressDetailId == null) {
                return false;
            }
            Integer resRow = addressesDao.addAddress(address, addAddressDetailId, userId);
            if (resRow == 0) {
                connection.rollback();
                return false;
            }
            connection.commit();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return false;
    }

    @Override
    public List<AddressEntity> getAddressesByUserId(int userId) {
        List<AddressEntity> addressEntityList = new ArrayList<>();
        for (Address address : addressesDao.getAddressesByUserId(userId)) {
            AddressEntity addressEntity = AddressEntity.builder()
                    .addressId(address.getAddressId())
                    .receiverName(address.getReceiverName())
                    .isDefault(address.getIsDefault()).build();
            AddressEntity tmp = addressesDetailDao.getAddressDetailByAddressId(address.getAddressDetailId());
            addressEntity.setPhone(tmp.getPhone());
            addressEntity.setProvince(tmp.getProvince());
            addressEntity.setCity(tmp.getCity());
            addressEntity.setDistrict(tmp.getDistrict());
            addressEntity.setDetailAddress(tmp.getDetailAddress());
            addressEntityList.add(addressEntity);
        }

        return addressEntityList;
    }

    @Override
    public int deleteAddress(int addressId, int userId) {
        for (Address address : addressesDao.getAddressesByUserId(userId)) {
            if (address.getAddressId() == addressId) {
                int row = addressesDetailDao.deleteById(address.getAddressDetailId());
                if (row == 0) {
                    return row;
                }
                return row;
            }
        }
        return -1;
    }

    @Override
    public int updAddress(AddressEntity address, int addressId, int userId) {
        try {
            connection.setAutoCommit(false);
            Integer addressDetailId = null;
            for (Address add : addressesDao.getAddressesByUserId(userId)) {
                if (add.getAddressId() == addressId) {
                    addressDetailId = add.getAddressDetailId();
                    break;
                }
            }
            if (addressDetailId == null) {
                return 0;
            }
            int row = addressesDao.updateReverseName(addressId, address);
            if (row == 0) {
                return row;
            }
            row = addressesDetailDao.updatePhone(address.getPhone(), addressDetailId);
            if (row == 0) {
                connection.rollback();
                return row;
            }
            connection.commit();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }
}
